package com.hnluchuan.bmjlearning.web.controller.admin;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hnluchuan.bmjlearning.common.CustomerType;
import com.hnluchuan.bmjlearning.common.ResultCode;
import com.hnluchuan.bmjlearning.common.ServicePackageType;
import com.hnluchuan.bmjlearning.common.UserType;
import com.hnluchuan.bmjlearning.dto.AgentDTO;
import com.hnluchuan.bmjlearning.dto.CustomerServicePackageDTO;
import com.hnluchuan.utils.common.DateUtils;
import com.hnluchuan.utils.exception.BizException;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hnluchuan.bmjlearning.dto.CustomerDTO;
import com.hnluchuan.bmjlearning.dto.UserDTO;
import com.hnluchuan.bmjlearning.service.CustomerService;

import com.hnluchuan.core.support.Page;
import com.hnluchuan.bmjlearning.web.controller.BaseController;

@Controller
@RequestMapping(value = "/admin/customer", produces="text/html;charset=UTF-8")
public class AdminCustomerController extends BaseController {
	
	@Autowired
	private CustomerService customerService;

	@RequestMapping(value = "/detail")
	public String detail(Map<String, Object> map, Long id) {
		if (id != null) {
			CustomerDTO dto = customerService.load(id);
			map.put("n", dto);
		}
		return "admin/customer/customer_detail";
	}
	
	@ResponseBody
	@RequestMapping(value = "/save")
	public String save(CustomerDTO dto) throws Exception {
		if (dto.getId() == null) {
			customerService.create(dto);
		} else {
			throw new RuntimeException("请实现更新逻辑，并删除该行");
			/*
			CustomerDTO old = customerService.load(dto.getId());
			// 确定哪些字段是在详情页面需要修改的，不在详情页修改的，就不需要在这里set值。updated和updatedBy不需要在这里处理，框架会统一处理
			old.setId(dto.getId());
			old.setUser(dto.getUser());
			old.setStatus(dto.getStatus());
			old.setOrgName(dto.getOrgName());
			old.setName(dto.getName());
			old.setExpireDate(dto.getExpireDate());
			old.setIdCard(dto.getIdCard());
			old.setMobile(dto.getMobile());
			old.setTitle(dto.getTitle());
			old.setProvince(dto.getProvince());
			old.setCity(dto.getCity());
			old.setDistrict(dto.getDistrict());
			old.setAddress(dto.getAddress());
			old.setSex(dto.getSex());
			old.setAgent(dto.getAgent());
			customerService.updateAllFields(old);
			*/
		}
		return ok();
	}

	@ResponseBody
	@RequestMapping(value = "/delete") 
	public String delete(Long[] ids) throws IOException {
		customerService.deleteByIds(ids);
		return ok();
	}
	
	@RequestMapping(value = "/list")
	public String list(CustomerDTO dto, Boolean search, Map<String, Object> map, Page page) {
		List<CustomerDTO> dtos = customerService.find(dto, page);
		map.put("list", dtos);
		map.put("query", dto);
		map.put("search", search);
		return "admin/customer/customer_list";
	}
	
	@RequestMapping(value = "/search")
	public String search(CustomerDTO dto, Map<String, Object> map, Page page) {
		List<CustomerDTO> dtos = customerService.find(dto, page);
		map.put("list", dtos);
		map.put("query", dto);
		return "admin/customer/customer_search";
	}
	
	/**
	 * 到在线学习的页面
	 * @return
	 * @throws Exception
	 */

	/**
	 * BMJ简介
	 * @param request
	 * @return
	 */
	@RequestMapping("/bmjIntroduction")
	public String bmjIntroduction(HttpServletRequest request){
		return "admin/bmj_introduction";
	}

	@RequestMapping(value = "/accountManageList")
	public String accountManageList(Map<String, Object> map, Page page, CustomerDTO customer) {
		UserDTO currentUser = getCurrentUserDTO();
		if(currentUser == null || currentUser.getType() != UserType.Manager.getValue()){
			throw new BizException(ResultCode.NotAuthorized.getRemark());
		}

		if(customer.getAgentId() != null){
			AgentDTO agent = agentService.load(customer.getAgentId());
			customer.setAgent(agent);
		}
		List<CustomerDTO> customerDTOList = customerService.find(customer, page);
		for(CustomerDTO c : customerDTOList){
			Date now = new Date();
			if (c.getExpireDate() != null) {
				Long days = getDateDiff(now, c.getExpireDate(), TimeUnit.DAYS);
				c.setRestDays(days.intValue());
			}
		}
		List<AgentDTO> agentDTOList = agentService.findAll();
		map.put("agentList", agentDTOList);
		map.put("list", customerDTOList);
		map.put("query", customer);
		map.put("types", CustomerType.values());
		return "admin/customer/agent_accountManageList";
	}

	@RequestMapping(value = "/accountManageDetail")
	public String accountManageDetail(Map<String, Object> map, Page page, Long id) {
		UserDTO currentUser = getCurrentUserDTO();
		if(currentUser == null || currentUser.getType() != UserType.Manager.getValue()){
			throw new BizException(ResultCode.NotAuthorized.getRemark());
		}

		CustomerDTO customerDTO = customerService.load(id);
		if(customerDTO == null){
			throw new BizException(ResultCode.InvalidParameter.getRemark());
		}
		map.put("customer", customerDTO);

		List<CustomerServicePackageDTO> customerServicePackageDTOList = customerServicePackageService.findByCustomerId(id);
		map.put("servicePackageList", customerServicePackageDTOList);

		Date now = new Date();
		if (customerDTO.getExpireDate() != null) {
			Long restDays = getDateDiff(now, customerDTO.getExpireDate(), TimeUnit.DAYS);
			map.put("restDays", restDays);
		}

		return "admin/customer/agent_accountManageDetail";
	}

	@ResponseBody
	@RequestMapping(value = "enableOrDisable", produces = "application/json;charset=utf-8")
	public String disableOrEnable(@RequestParam Long id, @RequestParam Integer status){
		UserDTO currentUser = getCurrentUserDTO();
		if(currentUser == null || currentUser.getType() != UserType.Manager.getValue()){
			throw new BizException(ResultCode.NotAuthorized.getRemark());
		}

		CustomerDTO customerDTO = customerService.load(id);
		if(customerDTO == null || customerDTO.getUser() == null){
			throw new BizException(ResultCode.InvalidParameter.getRemark());
		}

		customerService.enableOrDisable(customerDTO.getUser().getId(), id, status);
		return ok();
	}
}
